import os

from PyQt5.QtCore import Qt, QSize, pyqtSignal, QObject, QTimer
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QWidget, QStackedWidget, QHBoxLayout, \
    QVBoxLayout, QApplication, QDesktopWidget, QMessageBox, QLineEdit, QFileDialog

from ui.interface import Ui_TimeUnlock
from onefile import *
import time
from api.WebTime import Nettime

class MainWindow(QWidget, Ui_TimeUnlock):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        # const
        self.setupUi(self)
        self.file_path = ''

        timer = QTimer(self)
        timer.timeout.connect(self.show_net_time)
        timer.start()

        self.pushButton.clicked.connect(self.select_file)
        self.pushButton_2.clicked.connect(self.cript)


    def show_net_time(self):
        # 获得当前时间时间戳
        now = int(time.time())
        # 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
        timeArray = time.localtime(now)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
        self.label_2.setText(str(otherStyleTime))

    def select_file(self):
        self.file_path,format = QFileDialog.getOpenFileName(self, "选择一个文件",'./',filter="需加密的文件(*.*);;加密文件 (*.lock )")
        if self.file_path:
            filename, fileformat = get_file_split(self.file_path)
            self.textBrowser.append(f'已选择文件{filename}。')
            if fileformat == '.lock':
                j,left_time = is_arrive_time(self.file_path)
                if j:
                    self.textBrowser.append(f'文件为已加密文件，时间已到，可以选择解锁！终于到了解锁的日子！')
                else:
                    self.textBrowser.append(f'文件为已加密文件，但解锁时间未到，您请回吧，不会帮你解开的。')
                    self.textBrowser.append(f'还需要{left_time}秒才能解锁,约{int(left_time / 86400)}天')
            else:
                self.textBrowser.append(f'填入密钥及解锁的时跨度后可开始加密。')

    def cript(self):
        '''
        先获取路径，再根据文件格式判断是加密还是解密
        '''
        mypath = self.file_path
        if mypath is '':
            self.warnning('请选择文件!')
            return False
        filename,fileformat = get_file_split(mypath)

        if fileformat == '.lock':
            if is_arrive_time(mypath):
                key = self.lineEdit.text()
                if key is '':
                    self.warnning('请填写密钥!')
                    return False
                decode_file(key)
                self.warnning('解密完成！')
                self.textBrowser.append('解密完成！解锁的文件在软件所在目录。')
            else:
                self.warnning('解密时间未到！')
        else:
            now_time = get_ntime()
            if now_time is 0:
                self.warnning('获取网络时间失败!')
                return False
            time_cross = self.lineEdit_2.text()
            if time_cross is '':
                self.warnning('请填写时间跨度!')
                return False
            key = self.lineEdit.text()
            if key is '':
                self.warnning('请填写密钥!')
                return False
            encode_file(mypath,now_time,time_cross,key)
            self.warnning('加密完成！')
            self.textBrowser.append('加密完成！请妥善保管secret.lock文件，将来可用于解锁。')

        self.file_path = ''  # 路径初始化


    def show_info(self):
        secret_path = './secret.lock'
        show_lock_info()
        print(is_arrive_time(secret_path))

    def warnning(self,message='错误！'):
        QMessageBox.warning(self, '提示', message)